#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import time

from model.mysql_model import MysqlElephantV2
from model.simlog import Logger

obj_ele = MysqlElephantV2()
finish_log = Logger('./log/finish.log', logging.WARNING, logging.DEBUG)


def updata_ele_task_status(tabname, status, task_id):
	sql = "update %s set status=%s  where id=%s" % (tabname, status, task_id)
	# print sql
	obj_ele.cud(sql)


def statr_type(comparetype):
	if comparetype == 0:
		tableName = 'elephant_v2.ele_task'
		suffix = ''
	elif comparetype == 1:
		tableName = 'elephant_v2.ele_task_panorama'
		suffix = '_panorama'
	elif comparetype == 2:
		tableName = 'elephant_v2.ele_task_veer'
		suffix = '_veer'
	
	data = {}
	data['tableName'] = tableName
	data['suffix'] = suffix
	return data


def get_task_status(comparetype):
	data = statr_type(comparetype)
	tableName = data['tableName']
	sql_check = """select id from %s where status=%s""" % (tableName, 70)
	num = obj_ele.find(sql_check, 3)
	if num == 0:
		# print num
		return False, tableName
	else:
		task_id = obj_ele.find(sql_check, 2)[0]
		print task_id
		return task_id, tableName


def migrate_one_to_two(task_id):
	sql = """insert into ele_result_two (spider_id,company_id,task_id,clue_id,url,pic_url,oss_url,is_cover,result_id,client_date,report_date,add_date)
								  (select spider_id,company_id,task_id,clue_id,url,pic_url,oss_url,is_cover,result_id,client_date,report_date,add_date from ele_result_one where task_id=%s and status =0 )""" % task_id
	
	print sql
	obj_ele.cud(sql)


def run():
	for i in range(3):
		print i
		task_id, tbname = get_task_status(i)
		if task_id == False:
			continue
		else:
			migrate_one_to_two(task_id)
			updata_ele_task_status(tbname, 90, task_id)
			finish_log.info(task_id)


while True:
	run()
	time.sleep(60)
	break
